{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "d7e4077b-75d6-4c89-9dc9-ee828892f281",
   "metadata": {},
   "outputs": [],
   "source": [
    "import hashlib\n",
    "import os\n",
    "import tarfile\n",
    "import zipfile\n",
    "import requests\n",
    "import pandas as pd\n",
    "import torch\n",
    "\n",
    "DATA_HUB = dict()\n",
    "DATA_URL = 'http://d2l-data.s3-accelerate.amazonaws.com/'\n",
    "\n",
    "\n",
    "def download(name, cache_dir=os.path.join('..', 'data')):\n",
    "    '''下载一个DATA_HUB中的文件，返回本地文件名'''\n",
    "    # assert后面是不满足条件时显示的字符串，如果想执行操作就try except\n",
    "    assert name in DATA_HUB, f\"{name} 不存在于 {DATA_HUB}.\"\n",
    "    # 根据传入的name获取url和密钥\n",
    "    url, sha1_hash = DATA_HUB[name]\n",
    "    os.makedirs(cache_dir, exist_ok=True)\n",
    "    # 将url按 '/' 切五花肉 获取五花肉块列表 返回最后一块肉\n",
    "    fname = os.path.join(cache_dir, url.split('/')[-1])\n",
    "\n",
    "    if os.path.exists(fname):\n",
    "        # 创建一个sha1加密的对象\n",
    "        sha1 = hashlib.sha1()\n",
    "        with open(fname, 'rb') as f:\n",
    "            while True:\n",
    "                data = f.read(1048576)  # 2的20次方bytes（字节） 即1MB\n",
    "                if not data:\n",
    "                    break  # 不用再判断密钥了直接给我去下载吧\n",
    "                sha1.update(data)\n",
    "        # 如果生成的密钥和之前DATA_HUB中保存的秘钥一样那么就直接返回其文件名，不用下载\n",
    "        if sha1.hexdigest() == sha1_hash:\n",
    "            print('Nep！文件已存在！')\n",
    "            return fname\n",
    "\n",
    "    # 那就只有下载了噻\n",
    "    print(f'Nep！正在从{url}中下载{fname}...')\n",
    "    r = requests.get(url, stream=True, verify=True)\n",
    "    with open(fname, 'wb') as f:\n",
    "        f.write(r.content)\n",
    "    return fname\n",
    "\n",
    "\n",
    "def download_extract(name, folder=None):\n",
    "    '''下载并解压zip/tar文件'''\n",
    "    fname = download(name)\n",
    "    base_dir = os.path.dirname(fname)  # 去掉文件名，返回目录base_dir\n",
    "    data_dir, ext = os.path.splitext(fname)  # 获取文件名和后缀名\n",
    "    if ext == '.zip':\n",
    "        fp = zipfile.ZipFile(fname, 'r')\n",
    "    elif ext in ('.tar', '.gz'):\n",
    "        fp = tarfile.open(fname, 'r')\n",
    "    else:\n",
    "        assert False, 'Nep! 只可以解压zip/tar哦！'\n",
    "    fp.extractall(base_dir)  # 解压到当前目录base_dir\n",
    "    return os.path.join(base_dir, folder) if folder else data_dir\n",
    "\n",
    "\n",
    "def download_all():\n",
    "    '''下载DATA_HUB中的所有文件'''\n",
    "    for name in DATA_HUB:\n",
    "        download(name) \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "7501c939-dd5a-4075-a4aa-da3b4ba3b003",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Nep！文件已存在！\n",
      "Nep！文件已存在！\n",
      "----------train_data.shape： (1460, 81)\n",
      "----------test_data.shape： (1459, 80)\n",
      "----------train_data.iloc：    Id  MSSubClass MSZoning  LotFrontage SaleType SaleCondition  SalePrice\n",
      "0   1          60       RL         65.0       WD        Normal     208500\n",
      "1   2          20       RL         80.0       WD        Normal     181500\n",
      "2   3          60       RL         68.0       WD        Normal     223500\n",
      "3   4          70       RL         60.0       WD       Abnorml     140000\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "(2919, 79)"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 1-定义要下载的文件\n",
    "DATA_HUB['kaggle_house_train'] = (\n",
    "    # 二元组分别对应其url和密钥\n",
    "    DATA_URL + 'kaggle_house_pred_train.csv',\n",
    "    '585e9cc93e70b39160e7921475f9bcd7d31219ce')\n",
    "\n",
    "DATA_HUB['kaggle_house_test'] = (\n",
    "    DATA_URL + 'kaggle_house_pred_test.csv',\n",
    "    'fa19780a7b011d9b009e8bff8e99922a8ee2eb90')\n",
    "\n",
    "# 2-进行文件下载\n",
    "train_data = pd.read_csv(download('kaggle_house_train'))\n",
    "test_data = pd.read_csv(download('kaggle_house_test'))\n",
    "\n",
    "# 3-查看数据格式\n",
    "print('----------train_data.shape：', train_data.shape)\n",
    "print('----------test_data.shape：', test_data.shape)\n",
    "\n",
    "# 4-查看前四个样本的前四个后最后两个特征，以及相应的标价\n",
    "print('----------train_data.iloc：', train_data.iloc[0:4, [0, 1, 2, 3, -3, -2, -1]])\n",
    "\n",
    "# 5-【数据预处理】我们将其id信息从数据集中删除\n",
    "# 去掉ID后的所有样本,都表示对于训练集/测试集的所有样本，从下标1一直到最后，使用pd的concat将二者合并\n",
    "all_features = pd.concat((train_data.iloc[:,1:-1], test_data.iloc[:, 1:]))\n",
    "all_features.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "5ce9cf8a-1707-4685-9b74-67f0f0a53e9c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>MSSubClass</th>\n",
       "      <th>MSZoning</th>\n",
       "      <th>LotFrontage</th>\n",
       "      <th>LotArea</th>\n",
       "      <th>Street</th>\n",
       "      <th>Alley</th>\n",
       "      <th>LotShape</th>\n",
       "      <th>LandContour</th>\n",
       "      <th>Utilities</th>\n",
       "      <th>LotConfig</th>\n",
       "      <th>...</th>\n",
       "      <th>ScreenPorch</th>\n",
       "      <th>PoolArea</th>\n",
       "      <th>PoolQC</th>\n",
       "      <th>Fence</th>\n",
       "      <th>MiscFeature</th>\n",
       "      <th>MiscVal</th>\n",
       "      <th>MoSold</th>\n",
       "      <th>YrSold</th>\n",
       "      <th>SaleType</th>\n",
       "      <th>SaleCondition</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0.067320</td>\n",
       "      <td>RL</td>\n",
       "      <td>-0.184443</td>\n",
       "      <td>-0.217841</td>\n",
       "      <td>Pave</td>\n",
       "      <td>NaN</td>\n",
       "      <td>Reg</td>\n",
       "      <td>Lvl</td>\n",
       "      <td>AllPub</td>\n",
       "      <td>Inside</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.285886</td>\n",
       "      <td>-0.063139</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>-0.089577</td>\n",
       "      <td>-1.551918</td>\n",
       "      <td>0.157619</td>\n",
       "      <td>WD</td>\n",
       "      <td>Normal</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>-0.873466</td>\n",
       "      <td>RL</td>\n",
       "      <td>0.458096</td>\n",
       "      <td>-0.072032</td>\n",
       "      <td>Pave</td>\n",
       "      <td>NaN</td>\n",
       "      <td>Reg</td>\n",
       "      <td>Lvl</td>\n",
       "      <td>AllPub</td>\n",
       "      <td>FR2</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.285886</td>\n",
       "      <td>-0.063139</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>-0.089577</td>\n",
       "      <td>-0.446848</td>\n",
       "      <td>-0.602858</td>\n",
       "      <td>WD</td>\n",
       "      <td>Normal</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0.067320</td>\n",
       "      <td>RL</td>\n",
       "      <td>-0.055935</td>\n",
       "      <td>0.137173</td>\n",
       "      <td>Pave</td>\n",
       "      <td>NaN</td>\n",
       "      <td>IR1</td>\n",
       "      <td>Lvl</td>\n",
       "      <td>AllPub</td>\n",
       "      <td>Inside</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.285886</td>\n",
       "      <td>-0.063139</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>-0.089577</td>\n",
       "      <td>1.026577</td>\n",
       "      <td>0.157619</td>\n",
       "      <td>WD</td>\n",
       "      <td>Normal</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0.302516</td>\n",
       "      <td>RL</td>\n",
       "      <td>-0.398622</td>\n",
       "      <td>-0.078371</td>\n",
       "      <td>Pave</td>\n",
       "      <td>NaN</td>\n",
       "      <td>IR1</td>\n",
       "      <td>Lvl</td>\n",
       "      <td>AllPub</td>\n",
       "      <td>Corner</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.285886</td>\n",
       "      <td>-0.063139</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>-0.089577</td>\n",
       "      <td>-1.551918</td>\n",
       "      <td>-1.363335</td>\n",
       "      <td>WD</td>\n",
       "      <td>Abnorml</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0.067320</td>\n",
       "      <td>RL</td>\n",
       "      <td>0.629439</td>\n",
       "      <td>0.518814</td>\n",
       "      <td>Pave</td>\n",
       "      <td>NaN</td>\n",
       "      <td>IR1</td>\n",
       "      <td>Lvl</td>\n",
       "      <td>AllPub</td>\n",
       "      <td>FR2</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.285886</td>\n",
       "      <td>-0.063139</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>-0.089577</td>\n",
       "      <td>2.131647</td>\n",
       "      <td>0.157619</td>\n",
       "      <td>WD</td>\n",
       "      <td>Normal</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1454</th>\n",
       "      <td>2.419286</td>\n",
       "      <td>RM</td>\n",
       "      <td>-2.069222</td>\n",
       "      <td>-1.043758</td>\n",
       "      <td>Pave</td>\n",
       "      <td>NaN</td>\n",
       "      <td>Reg</td>\n",
       "      <td>Lvl</td>\n",
       "      <td>AllPub</td>\n",
       "      <td>Inside</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.285886</td>\n",
       "      <td>-0.063139</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>-0.089577</td>\n",
       "      <td>-0.078492</td>\n",
       "      <td>-1.363335</td>\n",
       "      <td>WD</td>\n",
       "      <td>Normal</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1455</th>\n",
       "      <td>2.419286</td>\n",
       "      <td>RM</td>\n",
       "      <td>-2.069222</td>\n",
       "      <td>-1.049083</td>\n",
       "      <td>Pave</td>\n",
       "      <td>NaN</td>\n",
       "      <td>Reg</td>\n",
       "      <td>Lvl</td>\n",
       "      <td>AllPub</td>\n",
       "      <td>Inside</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.285886</td>\n",
       "      <td>-0.063139</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>-0.089577</td>\n",
       "      <td>-0.815205</td>\n",
       "      <td>-1.363335</td>\n",
       "      <td>WD</td>\n",
       "      <td>Abnorml</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1456</th>\n",
       "      <td>-0.873466</td>\n",
       "      <td>RL</td>\n",
       "      <td>3.884968</td>\n",
       "      <td>1.246594</td>\n",
       "      <td>Pave</td>\n",
       "      <td>NaN</td>\n",
       "      <td>Reg</td>\n",
       "      <td>Lvl</td>\n",
       "      <td>AllPub</td>\n",
       "      <td>Inside</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.285886</td>\n",
       "      <td>-0.063139</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>-0.089577</td>\n",
       "      <td>1.026577</td>\n",
       "      <td>-1.363335</td>\n",
       "      <td>WD</td>\n",
       "      <td>Abnorml</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1457</th>\n",
       "      <td>0.655311</td>\n",
       "      <td>RL</td>\n",
       "      <td>-0.312950</td>\n",
       "      <td>0.034599</td>\n",
       "      <td>Pave</td>\n",
       "      <td>NaN</td>\n",
       "      <td>Reg</td>\n",
       "      <td>Lvl</td>\n",
       "      <td>AllPub</td>\n",
       "      <td>Inside</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.285886</td>\n",
       "      <td>-0.063139</td>\n",
       "      <td>NaN</td>\n",
       "      <td>MnPrv</td>\n",
       "      <td>Shed</td>\n",
       "      <td>1.144116</td>\n",
       "      <td>0.289865</td>\n",
       "      <td>-1.363335</td>\n",
       "      <td>WD</td>\n",
       "      <td>Normal</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1458</th>\n",
       "      <td>0.067320</td>\n",
       "      <td>RL</td>\n",
       "      <td>0.201080</td>\n",
       "      <td>-0.068608</td>\n",
       "      <td>Pave</td>\n",
       "      <td>NaN</td>\n",
       "      <td>Reg</td>\n",
       "      <td>Lvl</td>\n",
       "      <td>AllPub</td>\n",
       "      <td>Inside</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.285886</td>\n",
       "      <td>-0.063139</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>-0.089577</td>\n",
       "      <td>1.763290</td>\n",
       "      <td>-1.363335</td>\n",
       "      <td>WD</td>\n",
       "      <td>Normal</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>2919 rows × 79 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "      MSSubClass MSZoning  LotFrontage   LotArea Street Alley LotShape  \\\n",
       "0       0.067320       RL    -0.184443 -0.217841   Pave   NaN      Reg   \n",
       "1      -0.873466       RL     0.458096 -0.072032   Pave   NaN      Reg   \n",
       "2       0.067320       RL    -0.055935  0.137173   Pave   NaN      IR1   \n",
       "3       0.302516       RL    -0.398622 -0.078371   Pave   NaN      IR1   \n",
       "4       0.067320       RL     0.629439  0.518814   Pave   NaN      IR1   \n",
       "...          ...      ...          ...       ...    ...   ...      ...   \n",
       "1454    2.419286       RM    -2.069222 -1.043758   Pave   NaN      Reg   \n",
       "1455    2.419286       RM    -2.069222 -1.049083   Pave   NaN      Reg   \n",
       "1456   -0.873466       RL     3.884968  1.246594   Pave   NaN      Reg   \n",
       "1457    0.655311       RL    -0.312950  0.034599   Pave   NaN      Reg   \n",
       "1458    0.067320       RL     0.201080 -0.068608   Pave   NaN      Reg   \n",
       "\n",
       "     LandContour Utilities LotConfig  ... ScreenPorch  PoolArea PoolQC  Fence  \\\n",
       "0            Lvl    AllPub    Inside  ...   -0.285886 -0.063139    NaN    NaN   \n",
       "1            Lvl    AllPub       FR2  ...   -0.285886 -0.063139    NaN    NaN   \n",
       "2            Lvl    AllPub    Inside  ...   -0.285886 -0.063139    NaN    NaN   \n",
       "3            Lvl    AllPub    Corner  ...   -0.285886 -0.063139    NaN    NaN   \n",
       "4            Lvl    AllPub       FR2  ...   -0.285886 -0.063139    NaN    NaN   \n",
       "...          ...       ...       ...  ...         ...       ...    ...    ...   \n",
       "1454         Lvl    AllPub    Inside  ...   -0.285886 -0.063139    NaN    NaN   \n",
       "1455         Lvl    AllPub    Inside  ...   -0.285886 -0.063139    NaN    NaN   \n",
       "1456         Lvl    AllPub    Inside  ...   -0.285886 -0.063139    NaN    NaN   \n",
       "1457         Lvl    AllPub    Inside  ...   -0.285886 -0.063139    NaN  MnPrv   \n",
       "1458         Lvl    AllPub    Inside  ...   -0.285886 -0.063139    NaN    NaN   \n",
       "\n",
       "     MiscFeature   MiscVal    MoSold    YrSold  SaleType  SaleCondition  \n",
       "0            NaN -0.089577 -1.551918  0.157619        WD         Normal  \n",
       "1            NaN -0.089577 -0.446848 -0.602858        WD         Normal  \n",
       "2            NaN -0.089577  1.026577  0.157619        WD         Normal  \n",
       "3            NaN -0.089577 -1.551918 -1.363335        WD        Abnorml  \n",
       "4            NaN -0.089577  2.131647  0.157619        WD         Normal  \n",
       "...          ...       ...       ...       ...       ...            ...  \n",
       "1454         NaN -0.089577 -0.078492 -1.363335        WD         Normal  \n",
       "1455         NaN -0.089577 -0.815205 -1.363335        WD        Abnorml  \n",
       "1456         NaN -0.089577  1.026577 -1.363335        WD        Abnorml  \n",
       "1457        Shed  1.144116  0.289865 -1.363335        WD         Normal  \n",
       "1458         NaN -0.089577  1.763290 -1.363335        WD         Normal  \n",
       "\n",
       "[2919 rows x 79 columns]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 6-【数据预处理-数值特征】将所有缺失值替换为相应特征的平均值。然后进行feature scaling（先标准化再替换会更方便，因为标准化后均值就是0了）\n",
    "# 假设不是'object'类的数据就是数值类，把所有数值特征提取出来\n",
    "numeric_feaures = all_features.dtypes[all_features.dtypes != 'object'].index\n",
    "# 对数值特征进行标准化(实际情况拿不到测试集的时候可以用训练集的均值方差代替)\n",
    "all_features[numeric_feaures] = all_features[numeric_feaures].apply(\n",
    "    lambda x: (x - x.mean()) / (x.std()))\n",
    "# 把NAN变成0(也就是变成均值，妙啊！)\n",
    "all_features[numeric_feaures] = all_features[numeric_feaures].fillna(0)\n",
    "all_features"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "b32021f0-63f4-4b9f-a8c7-29eeaf936de4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>MSSubClass</th>\n",
       "      <th>LotFrontage</th>\n",
       "      <th>LotArea</th>\n",
       "      <th>OverallQual</th>\n",
       "      <th>OverallCond</th>\n",
       "      <th>YearBuilt</th>\n",
       "      <th>YearRemodAdd</th>\n",
       "      <th>MasVnrArea</th>\n",
       "      <th>BsmtFinSF1</th>\n",
       "      <th>BsmtFinSF2</th>\n",
       "      <th>...</th>\n",
       "      <th>SaleType_Oth</th>\n",
       "      <th>SaleType_WD</th>\n",
       "      <th>SaleType_nan</th>\n",
       "      <th>SaleCondition_Abnorml</th>\n",
       "      <th>SaleCondition_AdjLand</th>\n",
       "      <th>SaleCondition_Alloca</th>\n",
       "      <th>SaleCondition_Family</th>\n",
       "      <th>SaleCondition_Normal</th>\n",
       "      <th>SaleCondition_Partial</th>\n",
       "      <th>SaleCondition_nan</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>-1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1454</th>\n",
       "      <td>2</td>\n",
       "      <td>-2</td>\n",
       "      <td>-1</td>\n",
       "      <td>-1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1455</th>\n",
       "      <td>2</td>\n",
       "      <td>-2</td>\n",
       "      <td>-1</td>\n",
       "      <td>-1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1456</th>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1457</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1458</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>2919 rows × 330 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "      MSSubClass  LotFrontage  LotArea  OverallQual  OverallCond  YearBuilt  \\\n",
       "0              0            0        0            0            0          1   \n",
       "1              0            0        0            0            2          0   \n",
       "2              0            0        0            0            0          0   \n",
       "3              0            0        0            0            0         -1   \n",
       "4              0            0        0            1            0          0   \n",
       "...          ...          ...      ...          ...          ...        ...   \n",
       "1454           2           -2       -1           -1            1          0   \n",
       "1455           2           -2       -1           -1            0          0   \n",
       "1456           0            3        1            0            1          0   \n",
       "1457           0            0        0            0            0          0   \n",
       "1458           0            0        0            0            0          0   \n",
       "\n",
       "      YearRemodAdd  MasVnrArea  BsmtFinSF1  BsmtFinSF2  ...  SaleType_Oth  \\\n",
       "0                0           0           0           0  ...             0   \n",
       "1                0           0           1           0  ...             0   \n",
       "2                0           0           0           0  ...             0   \n",
       "3                0           0           0           0  ...             0   \n",
       "4                0           1           0           0  ...             0   \n",
       "...            ...         ...         ...         ...  ...           ...   \n",
       "1454             0           0           0           0  ...             0   \n",
       "1455             0           0           0           0  ...             0   \n",
       "1456             0           0           1           0  ...             0   \n",
       "1457             0           0           0           0  ...             0   \n",
       "1458             0           0           0           0  ...             0   \n",
       "\n",
       "      SaleType_WD  SaleType_nan  SaleCondition_Abnorml  SaleCondition_AdjLand  \\\n",
       "0               1             0                      0                      0   \n",
       "1               1             0                      0                      0   \n",
       "2               1             0                      0                      0   \n",
       "3               1             0                      1                      0   \n",
       "4               1             0                      0                      0   \n",
       "...           ...           ...                    ...                    ...   \n",
       "1454            1             0                      0                      0   \n",
       "1455            1             0                      1                      0   \n",
       "1456            1             0                      1                      0   \n",
       "1457            1             0                      0                      0   \n",
       "1458            1             0                      0                      0   \n",
       "\n",
       "      SaleCondition_Alloca  SaleCondition_Family  SaleCondition_Normal  \\\n",
       "0                        0                     0                     1   \n",
       "1                        0                     0                     1   \n",
       "2                        0                     0                     1   \n",
       "3                        0                     0                     0   \n",
       "4                        0                     0                     1   \n",
       "...                    ...                   ...                   ...   \n",
       "1454                     0                     0                     1   \n",
       "1455                     0                     0                     0   \n",
       "1456                     0                     0                     0   \n",
       "1457                     0                     0                     1   \n",
       "1458                     0                     0                     1   \n",
       "\n",
       "      SaleCondition_Partial  SaleCondition_nan  \n",
       "0                         0                  0  \n",
       "1                         0                  0  \n",
       "2                         0                  0  \n",
       "3                         0                  0  \n",
       "4                         0                  0  \n",
       "...                     ...                ...  \n",
       "1454                      0                  0  \n",
       "1455                      0                  0  \n",
       "1456                      0                  0  \n",
       "1457                      0                  0  \n",
       "1458                      0                  0  \n",
       "\n",
       "[2919 rows x 330 columns]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 7-【数据预处理-离散值特征】进行one-hot独热编码替换\n",
    "all_features = pd.get_dummies(all_features, dummy_na=True)\n",
    "# 将布尔值转换为0和1\n",
    "all_features = all_features.astype(int)\n",
    "all_features"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "d21af53d-7211-44b2-b419-8792ac5bea75",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "all_features 中的所有值都是数值类型\n"
     ]
    }
   ],
   "source": [
    "# 假设 all_features 是一个 DataFrame\n",
    "non_numeric_columns = all_features.select_dtypes(exclude=['number']).columns\n",
    "\n",
    "if non_numeric_columns.empty:\n",
    "    print(\"all_features 中的所有值都是数值类型\")\n",
    "else:\n",
    "    print(\"all_features 中存在非数值类型的列:\", non_numeric_columns.tolist())\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "9c3d8e52-1ef3-4f52-ae9a-4823651654d2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "----------train_features.shape： torch.Size([1460, 330])\n",
      "----------train_labels.shape： torch.Size([1460, 1])\n",
      "----------test_features.shape： torch.Size([1459, 330])\n"
     ]
    }
   ],
   "source": [
    "# 8-【数据预处理-分割数据集】将数据集分割为训练集和测试集\n",
    "n_train = train_data.shape[0]\n",
    "# 从all_features中取出前n_train行作为训练集，后n_test行作为测试集\n",
    "train_features = torch.tensor(all_features[:n_train].values, dtype=torch.float32)\n",
    "test_features = torch.tensor(all_features[n_train:].values, dtype=torch.float32)\n",
    "# 从train_data中取出前n_train行作为训练集，后n_test行作为测试集\n",
    "train_labels = torch.tensor(train_data.SalePrice.values, dtype=torch.float32).view(-1, 1)\n",
    "\n",
    "# 9-【数据预处理-查看数据】查看数据\n",
    "print('----------train_features.shape：', train_features.shape)\n",
    "print('----------train_labels.shape：', train_labels.shape)\n",
    "print('----------test_features.shape：', test_features.shape)\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
